home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr43 / sbdsp2b.zip / RPD2VOC.PAS < prev    next >
Pascal/Delphi Source File  |  1995-01-23  |  8KB  |  212 lines

  1. Program RPD2VOC;
  2. Uses SBDSP, Crt;
  3. (**************************************************************************)
  4. (*                            Program RPD2VOC                             *)
  5. (*                       By: Romesh Prakashpalan                          *)
  6. (**************************************************************************)
  7. (* NOTE: This program will only convert RPD files that are up to approx   *)
  8. (* 15MB in length!!                                                       *)
  9. (**************************************************************************)
  10.  
  11. type
  12.   RPDHeader = Record
  13.                 Sig: Array [0..2] of Char; (* "RPD" *)
  14.                 Version: Word;             (* Version # *)
  15.                 DAC: Byte;                 (* 8/16/4/4.6/2/2.6, etc...*)
  16.                 Phase: Byte;               (* Mono=0, Stereo=1, Surround=2*)
  17.                 Freq: Word;                (* Sample Frequency *)
  18.                 Channels: Byte;            (* # of DIGITAL Channels *)
  19.                 ChannelMethod: Byte;       (* Method for laying down channels *)
  20.                 Size: LongInt;             (* Size of Sample *)
  21.                 Reserved: Array [0..31] of Byte;
  22.               end;
  23.  
  24.   VOCHeader = Record
  25.                 Sig:Array [0..$13] of Char;(* "Creative Voice File" *)
  26.                 Offset: Word;              (* Offset of first datablock in the*)
  27.                                            (* .VOC file.                      *)
  28.                 Version: Word;             (* Version # *)
  29.                 Version2s: Word;           (* 2's complement of version # plus*)
  30.                                            (* 1234h ex: 1.10 = 1129           *)
  31.               end;
  32.  
  33.   ThreeByte = Array [1..3] of Byte;
  34.  
  35.   Block1Type = Record                      (* Voice Data Block *)
  36.                 Length: ThreeByte;
  37.                 TimeConstant: Byte;        (* = 256 - 1000000/Sample Rate *)
  38.                 PackType: Byte;            (* 0 = 8-bit unpacked *)
  39.                                            (* 1 = 4-bit packed   *)
  40.                                            (* 2 = 2.6 bit packed *)
  41.                                            (* 3 = 2-bit packed   *)
  42.                end;
  43.   Block2Type = Record                      (* Voice Continuation *)
  44.                  Length: ThreeByte;
  45.                end;
  46.  
  47.   Block3Type = Record                      (* Silence block *)
  48.                  Length: ThreeByte;        (* Always 3 *)
  49.                  Pause: Word;              (* Pause period in sample bytes *)
  50.                  TimeConstant: Byte;
  51.                end;
  52.  
  53.   Block4Type = Record                      (* Marker Block *)
  54.                  Length: ThreeByte;
  55.                  MarkerValue: Word;
  56.                end;
  57.  
  58.   Block5Type = Record                      (* Ascii Text (null-terminated) *)
  59.                  Length: ThreeByte;
  60.                end;
  61.  
  62.   Block6Type = Record                      (* Repeat Loop *)
  63.                  Length: ThreeByte;        (* Always 2 *)
  64.                  Count: Word;              (* 1 to $FFFE, $FFFF = endless loop*)
  65.                end;
  66.  
  67.   Block7Type = Record                      (* End Repeat Loop *)
  68.                  Length: ThreeByte;        (* Always 0 *)
  69.                end;
  70.  
  71.   Block8Type = Record                      (* Extended Block *)
  72.                  Length: ThreeByte;        (* Always 4 *)
  73.                  TimeConstant: Word;       (* For Mono:  *)
  74.                                            (* 65535-(256,000,000/sample rate)*)
  75.                                            (* For Stereo: *)
  76.                                            (* 65535-(256000000/sample rate*2) *)
  77.                  PackType: Byte;           (* Same as Block 1 *)
  78.                  Mode: Byte;               (* 0 = Mono, 1 = Stereo *)
  79.                end;
  80.  
  81.   Block9Type = Record                      (* NEW Extended VOC Block *)
  82.                  Length: ThreeByte;        (* Length of Sample + 12 (Bytes) *)
  83.                  SamplesPerSec: LongInt;   (* ACTUAL Samples/Second *)
  84.                  BitsPerSample: Byte;      (* Bits/Sample after compression *)
  85.                  Channels: Byte;           (* 1 for mono, 2 for stereo *)
  86.                  FormatTag: Word;          (* Format Tags follow: *)
  87.                                            (*   $000 - 8 bit unsigned PCM *)
  88.                                            (*   $001 - 4 Bit ADPCM *)
  89.                                            (*   $002 - 2.6 Bit ADPCM *)
  90.                                            (*   $003 - 2 Bit ADPCM *)
  91.                                            (*   $004 - 16 Bit Signed PCM *)
  92.                                            (*   $006 - CCITT a-Law *)
  93.                                            (*   $007 - CCITT u-Law *)
  94.                                            (*   $200 - 16 bit -> 4 Bit ADPCM *)
  95.                  Reserved: LongInt;        (* Reserved by Creative Labs *)
  96.                end;
  97.  
  98. var
  99.   Source, Destination: String;
  100.   Ch: Char;
  101.  
  102. Function FileExists (Filename: String): Boolean;
  103. var
  104.  F: file;
  105. Begin
  106.  {$I-}
  107.  Assign(F, FileName);
  108.  FileMode := 0;      (* Set file access to read only *)
  109.  Reset(F);
  110.  Close(F);
  111.  {$I+}
  112.  FileExists := (IOResult = 0) and (FileName <> '');
  113. End;
  114.  
  115. Procedure Long2ThreeByte (Num: Longint; var Result: ThreeByte);
  116. Begin
  117.   Move (Ptr (Seg (Num), Ofs (Num))^, Result, SizeOf (Result));
  118. End;
  119.  
  120. Function ThreeByte2Long (TheData: ThreeByte): LongInt;
  121. Begin
  122.   ThreeByte2Long := LongInt (TheData[1]) + LongInt (TheData[2]) shl 8 +
  123.                     LongInt (TheData[3]) shl 16;
  124. End;
  125.  
  126. Procedure RPDToVoc (Source, Destination: String);
  127. var
  128.   SFile, DFile: File;
  129.   TempVOCHeader: VOCHeader;
  130.   TempRPDHeader: RPDHeader;
  131.   TempString: String;
  132.   I: Integer;
  133.   Block9: Block9Type;  (* Save as the MOST RECENT type of block available! *)
  134.   TempThreeByte: ThreeByte;
  135.   TempBuffer: Pointer;
  136.   LeftToGo: LongInt;
  137.   TempByte: Byte;
  138. Begin
  139.   GetMem (TempBuffer, $FFFF);
  140.   Assign (SFile, Source);
  141.   Assign (DFile, Destination);
  142.   Reset (SFile, 1);
  143.   Rewrite (DFile, 1);
  144.   TempString := 'Creative Voice File';
  145.   For I := 1 to Length (TempString) do
  146.     TempVOCHeader.Sig[I-1] := TempString[I];
  147.   TempVOCHeader.Offset := SizeOf (TempVOCHeader);
  148.   TempVOCHeader.Version := $A01;
  149.   TempVOCHeader.Version2s := $2911;
  150.   BlockWrite (DFile, TempVOCHeader, SizeOf (TempVOCHeader));
  151.   BlockRead (SFile, TempRPDHeader, SizeOf (TempRPDHeader));
  152.   LeftToGo := TempRPDHeader.Size;
  153.   Long2ThreeByte (TempRPDHeader.Size - 12 - SizeOf (TempRPDHeader),
  154.                   TempThreeByte);
  155.   Block9.Length := TempThreeByte;
  156.   Block9.SamplesPerSec := TempRPDHeader.Freq;
  157.   Case TempRPDHeader.DAC of
  158.     EightBitDMA:   Block9.FormatTag := $000;
  159.     FourBitDMA:    Block9.FormatTag := $001;
  160.     TwoSixBitDMA:  Block9.FormatTag := $002;
  161.     TwoBitDMA:     Block9.FormatTag := $003;
  162.     SixteenBitDMA: Block9.FormatTag := $004;
  163.   end;
  164.   Block9.Channels := TempRPDHeader.Channels + 1;
  165.   If TempRPDHeader.Channels = Stereo then
  166.     Block9.SamplesPerSec := Block9.SamplesPerSec div 2;
  167.  
  168.   Block9.Reserved := 0;
  169.   TempByte := 9;
  170.   BlockWrite (DFile, TempByte, 1);
  171.   BlockWrite (DFile, Block9, SizeOf (Block9));
  172.   Repeat
  173.     If LeftToGo > $FFFF then
  174.     Begin
  175.       Dec (LeftToGo, $FFFF);
  176.       BlockRead (SFile, TempBuffer^, $FFFF);
  177.       BlockWrite (DFile, TempBuffer^, $FFFF);
  178.     End
  179.     Else
  180.     Begin
  181.       BlockRead (SFile, TempBuffer^, LeftToGo);
  182.       BlockWrite (DFile, TempBuffer^, LeftToGo);
  183.       LeftToGo := 0;
  184.     End;
  185.     Write ('.');
  186.   Until LeftToGo = 0;
  187.   TempByte := 0;
  188.   BlockWrite (DFile, TempByte, 1);
  189. End;
  190.  
  191. Begin
  192.   Clrscr;
  193.   WriteLn ('           RPD2VOC version 1.00, By: Romesh Prakashpalan, 1995');
  194.   WriteLn ('                            RPD2VOC is FREEWARE             ');
  195.   Write ('Enter in RPD file to be converted: ');
  196.   ReadLn (Source);
  197.   If not FileExists (Source) then
  198.   Begin
  199.     WriteLn ('Source File Doesn''t Exist!');
  200.     Halt;
  201.   End;
  202.   Write ('Enter in VOC file to convert to: ');
  203.   ReadLn (Destination);
  204.   If FileExists (Destination) then
  205.   Begin
  206.     Write ('File exists! overwrite? (''N'' for No, any other key kills it): ');
  207.     Ch := UpCase (Readkey);
  208.     WriteLn (Ch);
  209.     If Ch = 'N' then Halt;
  210.   End;
  211.   RPDToVOC (Source, Destination);
  212. End.